package com.mesnac.filemgr.biz;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.mesnac.comm.sysinfo.SysInfo;
import com.mesnac.comm.util.DateFormat;
import com.mesnac.filemgr.entity.DirMgr;
import com.mesnac.filemgr.entity.DirectoryInfor;
import com.mesnac.filemgr.entity.FileInfor;
import com.mesnac.filemgr.entity.SearchCondition;
import com.mesnac.sort.SortList;

public class JsonDirByPath {

	public static JSONArray getJsonDirByPath(String path) throws Exception {

		// 这里我们只做两层的目录
		DirectoryInfor root = DirMgr.dirDeserialize(path);
		List<DirectoryInfor> a = root.getDirList();
		List<Node> result = new ArrayList<Node>();
		for (int i = 0; i < a.size(); i++) {
			Node tmp = new Node(a.get(i).getDirName(), a.get(i).getDirName(),
					true);
			String childPath = a.get(i).getDirPath();
			DirectoryInfor childroot = DirMgr.dirDeserialize(childPath);
			List<DirectoryInfor> childa = childroot.getDirList();
			List<Leaf> childList = new ArrayList<Leaf>();
			for (int j = 0; j < childa.size(); j++) {
				Leaf childtmp = new Leaf(childa.get(j).getDirName(), childa
						.get(j).getDirName());
				// childtmp.setChildren(null);
				childList.add(childtmp);
			}
			tmp.setChildren(childList);
			result.add(tmp);
		}

		JSONArray jsonArray = JSONArray.fromObject(result);
		return jsonArray;
	}

	/**
	 * 获得目录下的文件和目录列表
	 * 
	 * @param path
	 * @return
	 * @throws Exception
	 */

	public static JSONObject getJsonDirAndFileByPath(String path, int start,
			int limit) throws Exception {
		DirectoryInfor root = DirMgr.dirDeserialize(path);
		List<DirectoryInfor> a = root.getDirList();
		List<FileInfor> b = root.getFileList();
		List<DirNode> result = new ArrayList<DirNode>();
		List<DirNode> bre = new ArrayList<DirNode>();
		DirNode tm = new DirNode();
		tm.setTitle("...返回上级目录");
		if (a != null) {
			for (int i = 0; i < a.size(); i++) {
				DirNode tmp = new DirNode();
				DirectoryInfor infor = a.get(i);
				tmp.setTitle(infor.getDirName());
				tmp.setContributor(null);
				tmp.setDescribe(null);
				tmp.setDownloadtimes(null);
				tmp.setSize(null);
				tmp.setType(null);
				tmp.setUploadtime(null);
				tmp.setVersion(null);
				result.add(tmp);
			}
		}
		if (b != null) {
			for (int i = 0; i < b.size(); i++) {
				DirNode tmp = new DirNode();
				FileInfor infor = b.get(i);
				tmp.setContributor(infor.getFilemaster());
				tmp.setDescribe(infor.getDescribe());
				tmp.setDownloadtimes(String.valueOf(infor.getDownloadtimes()));
				tmp.setSize(infor.getFileSize());
				tmp.setTitle(infor.getFileName());
				tmp.setType(infor.getFileType());
				tmp.setUploadtime(DateFormat.getDate(infor.getUploadtime()));
				tmp.setVersion(String.valueOf(infor.getVersion()));
				bre.add(tmp);
			}
		}
		DirResult re = new DirResult();
		int total = 1;
		if (a != null)
			total += a.size();
		if (b != null)
			total += b.size();
		re.setTotalCount(total);
		List<DirNode> relimit = new ArrayList<DirNode>();
		relimit.add(tm);
		SortList<DirNode> slist = new SortList<DirNode>();
		slist.Sort(bre, "getUploadtime", "desc");
		for(int i = 0; i < bre.size(); i++){
			result.add(bre.get(i));
		}
		
		for (int i = start; i < result.size() && i < start + limit; i++) {
			relimit.add(result.get(i));
		}
		re.setFilelist(relimit);
		JSONObject jsonArray = JSONObject.fromObject(re);
		return jsonArray;
	}

	/**
	 * 搜索目录下的文件
	 * 
	 * @param path
	 * @param start
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	public static JSONObject searchFileByName(String path, int start,
			int limit, String searchname) throws Exception {
		DirectoryInfor root = DirMgr.dirDeserialize(path);
		List<DirectoryInfor> a = root.getDirList();
		List<FileInfor> b = root.getFileList();
		List<DirNode> result = new ArrayList<DirNode>();
		List<DirNode> bre = new ArrayList<DirNode>();
		DirNode tm = new DirNode();
		tm.setTitle("...返回上级目录");
		if (a != null) {
			for (int i = 0; i < a.size(); i++) {
				DirNode tmp = new DirNode();
				DirectoryInfor infor = a.get(i);
				tmp.setTitle(infor.getDirName());
				tmp.setContributor(null);
				tmp.setDescribe(null);
				tmp.setDownloadtimes(null);
				tmp.setSize(null);
				tmp.setType(null);
				tmp.setUploadtime(null);
				tmp.setVersion(null);
				if (tmp.getTitle().contains(searchname))
					result.add(tmp);
			}
		}
		if (b != null) {
			for (int i = 0; i < b.size(); i++) {
				DirNode tmp = new DirNode();
				FileInfor infor = b.get(i);
				tmp.setContributor(infor.getFilemaster());
				tmp.setDescribe(infor.getDescribe());
				tmp.setDownloadtimes(String.valueOf(infor.getDownloadtimes()));
				tmp.setSize(infor.getFileSize());
				tmp.setTitle(infor.getFileName());
				tmp.setType(infor.getFileType());
				tmp.setUploadtime(DateFormat.getDate(infor.getUploadtime()));
				tmp.setVersion(String.valueOf(infor.getVersion()));
				if (tmp.getTitle().contains(searchname))
					bre.add(tmp);
			}
		}
		DirResult re = new DirResult();
		int total = 1;
		if (a != null)
			total += a.size();
		if (b != null)
			total += b.size();
		re.setTotalCount(total);
		List<DirNode> relimit = new ArrayList<DirNode>();
		relimit.add(tm);
		// result按照时间逆序排列,原来result本身是有序的，因此做对调即可
		SortList<DirNode> slist = new SortList<DirNode>();
		slist.Sort(bre, "getUploadtime", "desc");
		for(int i = 0; i < bre.size(); i++){
			result.add(bre.get(i));
		}
		for (int i = start; i < result.size() && i < start + limit; i++) {
			relimit.add(result.get(i));
		}
		re.setFilelist(relimit);
		JSONObject jsonArray = JSONObject.fromObject(re);
		return jsonArray;
	}

	/*
	 * 
	 * 根据条件搜索结果
	 */
	public static JSONObject searchFileByCondition(SearchCondition condition,
			int start, int limit) throws Exception {
		DirResult re = new DirResult();
		List<DirNode> result = new ArrayList<DirNode>();
		String root = SysInfo.rootpath;
		searchAllFileByCondition(condition, result, root);
		int total = result.size();
		re.setTotalCount(total);
		List<DirNode> relimit = new ArrayList<DirNode>();

		for (int i = start; i < result.size() && i < start + limit; i++) {
			relimit.add(result.get(i));
		}
		re.setFilelist(relimit);
		JSONObject jsonArray = JSONObject.fromObject(re);
		return jsonArray;
	}

	private static void searchAllFileByCondition(SearchCondition condition,
			List<DirNode> result, String path) throws Exception {
		//System.out.println("path=="+path);
		
		DirectoryInfor root = DirMgr.dirDeserialize(path);
		List<DirectoryInfor> a = root.getDirList();
		List<FileInfor> b = root.getFileList();

		if (a != null) {
			for (int i = 0; i < a.size(); i++) {
				DirectoryInfor tmp = a.get(i);
				String childpath = tmp.getDirPath();
				searchAllFileByCondition(condition, result, childpath);
			}
		}
		if (b != null) {
			for (int i = 0; i < b.size(); i++) {
				DirNode tmp = new DirNode();
				FileInfor infor = b.get(i);
				tmp.setContributor(infor.getFilemaster());
				tmp.setDescribe(infor.getDescribe());
				tmp.setDownloadtimes(String.valueOf(infor.getDownloadtimes()));
				tmp.setSize(infor.getFileSize());
				tmp.setTitle(infor.getFileName());
				tmp.setType(infor.getFileType());
				tmp.setUploadtime(DateFormat.getDate(infor.getUploadtime()));
				tmp.setVersion(String.valueOf(infor.getVersion()));
				tmp.setKeywords(infor.getKeywords());
				//System.out.println(infor.getKeywords());
				// 路径这里要去掉根路径和最后的文件名
				int start = infor.getFilePath().indexOf("\\", 4);
				int end = infor.getFilePath().lastIndexOf("\\");
				tmp.setFpath(infor.getFilePath().substring(start + 1, end));
				// 条件判断
				if (condition.getFilename() == null
						|| tmp.getTitle().contains(condition.getFilename())) {
					if (condition.getContributor() == null
							|| tmp.getContributor() == null
							|| tmp.getContributor().contains(
									condition.getContributor())) {
						if (condition.getKeywords() == null
								|| (tmp.getKeywords()!=null&&tmp.getKeywords().contains(
										condition.getKeywords().trim()))) {
							//System.out.println("here is keywords");
							//System.out.println(condition.getKeywords()+"-"+tmp.getKeywords());
							if (condition.getType() == null
									|| condition.getType().contains(
											tmp.getType())) {
								if (condition.getBegintime() == null
										|| DateFormat.parseStr(
												tmp.getUploadtime()).after(
												condition.getBegintime())
										|| DateFormat.parseStr(
												tmp.getUploadtime()).equals(
												condition.getBegintime())) {
									if (condition.getEndtime() == null
											|| DateFormat
													.parseStr(
															tmp.getUploadtime())
													.before(
															condition
																	.getEndtime())
											|| DateFormat
													.parseStr(
															tmp.getUploadtime())
													.equals(
															condition
																	.getBegintime())) {
										//System.out.println("result"+tmp.getKeywords());
										result.add(tmp);
									}
								}
							}
						}
					}
				}

			}
		}
	}

	public static void main(String args[]) throws Exception {
		// JSONArray ja = new
		// JsonDirByPath().getJsonDirByPath("D:\\workspace\\pinshuoinfor\\WebRoot\\pinshuo");
		// System.out.println(ja);
		// String path = "D:\\pinshuo\\项目管理\\橡胶谷";
		// System.out.println(path);
		// JSONObject ja = new JsonDirByPath().getJsonDirAndFileByPath(
		// "D:\\pinshuo\\项目管理\\橡胶谷", 0, 5);
		// System.out.println(ja);
		//long begin = System.currentTimeMillis();
		//List<DirNode> result = new ArrayList<DirNode>();
		SearchCondition condition = new SearchCondition();
		condition.setKeywords("哈哈");
		JsonDirByPath.searchFileByCondition(condition, 0, 10);
		//condition.setFilename("1");
		// condition.setType("doc");
		// condition.setContributor("adadf");
		// condition.setBegintime(new Date());
		//JsonDirByPath.searchFileByCondition(condition, 0, 10);
		//String a = "wdfdfdfdf";
		//System.out.println(a.contains("wd"));
		//long end = System.currentTimeMillis();
		//System.out.println(end - begin);
	}

}
